﻿#include "HLMHub.h"
#include <qlabel.h>
#include <qmessagebox.h>
#include "FramelessHelper/Widgets/standardtitlebar.h"
#include "FramelessHelper/Widgets/framelesswidgetshelper.h"
#include "FramelessHelper/Widgets/framelesshelperwidgets_global.h"
#include "FramelessHelper/Core/framelesshelpercore_global.h"

#pragma execution_character_set("utf-8")
#define PORT 20231

HLMHub::HLMHub(QWidget *parent)
    : FramelessMainWindow(parent)
{
    ui.setupUi(this);

    //this->setWindowTitle("XXHub");
    this->setWindowIcon(QIcon(":/HLMHub/image/H-桌面-logo.png"));
    StandardTitleBar* titleBar = new StandardTitleBar(this);
    titleBar->setObjectName("titleBar");
    setMenuWidget(titleBar);
    FramelessWidgetsHelper* helper = FramelessWidgetsHelper::get(this);
    helper->setSystemButton(titleBar->minimizeButton(), Global::SystemButtonType::Minimize);
    helper->setSystemButton(titleBar->maximizeButton(), Global::SystemButtonType::Maximize);
    helper->setSystemButton(titleBar->closeButton(), Global::SystemButtonType::Close);
    helper->setTitleBarWidget(titleBar);
    ChromePalette* palette = titleBar->chromePalette();
    palette->setTitleBarActiveBackgroundColor("#007FFF");
    palette->setTitleBarActiveForegroundColor("#FFFFFF");
    palette->setTitleBarInactiveBackgroundColor("#007FFF");
    palette->setTitleBarInactiveForegroundColor("#FFFFFF");
    QFont font("Microsoft YaHei");
    font.setPixelSize(22);
    titleBar->setTitleFont(font);
    titleBar->setWindowIconVisible(false);
    titleBar->setFixedHeight(40);

    QLabel* labCopyright = new QLabel("长沙科梁科技有限公司  Copyright©2023-2024 科梁编程挑战赛 版权所有");
    labCopyright->setAlignment(Qt::AlignCenter);
    statusBar()->insertPermanentWidget(1, labCopyright, 1);

    m_plot = new QCustomPlot(this);
    setCentralWidget(m_plot);
    m_plot->plotLayout()->clear();
    m_plot->plotLayout()->setWrap(4);

    m_server = new QTcpServer(this);
    QObject::connect(m_server, &QTcpServer::newConnection, this, &HLMHub::onNewConnection);

    if (!m_server->listen(QHostAddress::AnyIPv4, PORT)) { 
        QMessageBox::warning(this, tr("服务初始化失败，端口:%1").arg(PORT), "确定");
    }
}

HLMHub::~HLMHub() { }

void HLMHub::updatePlotLayout() 
{
    m_plot->plotLayout()->clear();
    for (int i = 0; i < m_samplerList.count(); i++)
    {
        SamplerView* pView = m_samplerList.at(i);
        m_plot->plotLayout()->addElement(pView->layout());
    }
}

void HLMHub::closeSamplerView()
{
    SamplerView* pSampler = qobject_cast<SamplerView*>(sender());
    if (pSampler) {
        //m_plot->plotLayout()->take(pSampler->layout());
        m_samplerList.removeOne(pSampler);
        pSampler->deleteLater();
        //updatePlotLayout();
    }
}

void HLMHub::onNewConnection()
{
    QTcpSocket* socket = m_server->nextPendingConnection();

    SamplerView* sampler = new SamplerView(m_plot, socket);
    connect(sampler, &SamplerView::closeRequest, this, &HLMHub::closeSamplerView);

    m_samplerList << sampler;
    m_plot->plotLayout()->addElement(sampler->layout());
    m_plot->replot();
}
